package mi.payment.settings;

import android.text.TextUtils;

import mi.payment.utils.SharedPreferencesUtil;

import static mi.payment.settings.MerManagerSettings.MERCHANT_MANAGER_KEY;

/**
 * 商户管理设置
 *
 * @author Xiho on 2017/1/9.
 */
public class MerchantSettingsOperation {

    private static MerchantSettingsOperation instance;
    private MerManagerSettings instanceSettings;

    public static MerchantSettingsOperation getInstance() {
        if (instance == null) {
            instance = new MerchantSettingsOperation();
        }
        return instance;
    }

    private MerchantSettingsOperation() {
        instanceSettings = getMerManagerSettings();
        if (instanceSettings == null) {
            instanceSettings = new MerManagerSettings();
            saveMerchantData(instanceSettings);
        }
    }

    public void init(String merchantNum, String merchantName, String merchantEnglishName, String terminalNum) {
        MerManagerSettings bean = getMerManagerSettings();
        bean.setMerchantNum(merchantNum);
        bean.setMerchantName(merchantName);
        bean.setMerchantEnglishName(merchantEnglishName);
        bean.setTerminalNum(terminalNum);
        saveMerchantData(bean);
    }

    /**
     * 获取商户信息
     */
    public MerManagerSettings getMerManagerSettings() {
        if(instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(MERCHANT_MANAGER_KEY);
            if (obj instanceof MerManagerSettings) {
                instanceSettings = (MerManagerSettings) obj;
            } else {
                instanceSettings = new MerManagerSettings();
                saveMerchantData(instanceSettings);
            }
        }
        return instanceSettings;
    }


    /**
     * 商户号码
     *
     * @param merchantNum 商户号
     */
    public void saveMerchantNum(String merchantNum) {
        if (TextUtils.isEmpty(merchantNum)) {
            return;
        }
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setMerchantNum(merchantNum);
        saveMerchantData(merchant);
    }

    /**
     * 设置商户的名称
     *
     * @param merchantName 商户中文名
     */
    public void saveMerchantName(String merchantName) {
        if (TextUtils.isEmpty(merchantName)) {
            return;
        }
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setMerchantName(merchantName);
        saveMerchantData(merchant);
    }

    /**
     * 设置英文名字
     *
     * @param merchantEnglishName 商户英文名
     */
    public void saveMerchantEnglishName(String merchantEnglishName) {
        if (TextUtils.isEmpty(merchantEnglishName)) {
            return;
        }
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setMerchantEnglishName(merchantEnglishName);
        saveMerchantData(merchant);
    }

    /**
     * 设置终端号码
     *
     * @param terminalNum 商户终端号
     */
    public void saveTerminalNum(String terminalNum) {
        if (TextUtils.isEmpty(terminalNum)) {
            return;
        }
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setTerminalNum(terminalNum);
        saveMerchantData(merchant);
    }

    public void saveMerchantData(MerManagerSettings merManagerSettings) {
        this.instanceSettings = merManagerSettings;
        SharedPreferencesUtil.saveObj(merManagerSettings, MERCHANT_MANAGER_KEY);
    }

    public void saveTerminalParamVersion(String terminalParamVersion) {
        if (TextUtils.isEmpty(terminalParamVersion)) {
            return;
        }
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setTerminalParamVersion(terminalParamVersion);
        saveMerchantData(merchant);
    }

    public void isPrintAfterSign(boolean need){
        MerManagerSettings merchant = getMerManagerSettings();
        merchant.setNeedPrintAfterSign(need);
        saveMerchantData(merchant);
    }
}
